package com.he.springbootmybatis.controller;

import com.he.springbootmybatis.mapper.BlogMapper;
import com.he.springbootmybatis.pojo.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class BlogController {

    @Autowired
    private BlogMapper blogMapper;

    @GetMapping("/queryBlogList")
    public List<Blog> queryBlogList(){
        List<Blog> blogList = blogMapper.queryBlogList();
        for (Blog blog : blogList) {
            System.out.println(blog);
        }
        return blogList;
    }

    @GetMapping("/addBlog")
    public String UpdateBlog(){
        Blog blog = new Blog();
        blog.setId(2);
        blog.setTitle("Redis");
        blog.setAuthor("何恒嘉");
        blog.setCreateTime(new Date());
        blog.setViews(5000);
        blogMapper.addBlog(blog);
        return "add-ok";
    }

    @GetMapping("/deleteBlog")
    public String deleteBlog(){
        blogMapper.deleteBlog(2);
        return "delete-ok";
    }

    @GetMapping("/updateBlog")
    public String updateBlog(){
        blogMapper.updateBlog(new Blog(2,"sql","何恒嘉",new Date(),2333));
        return "update-ok";
    }

    @GetMapping("/selectBlog")
    public List<Blog> selectBlog(){
        List<Blog> blogById = blogMapper.queryBlogById(2);
        for (Blog blog : blogById) {
            System.out.println(blog);
        }
        return blogById;
    }
}
